package com.caregiver.watch.business.user.model.dto;

import com.caregiver.watch.business.user.enums.UserAddressStautsEnum;
import com.caregiver.watch.business.user.model.po.UserAddress;
import com.caregiver.watch.common.exception.BaseException;
import com.caregiver.watch.common.exception.business.BusinessException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import static com.caregiver.watch.common.constant.NumberConstants.ONE;
import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 绑定收货地址参数
 * @Author 疆戟
 * @Date 2025/8/17 11:30
 * @Version 1.0
 */
@Data
@Schema(title = "绑定收货地址参数")
public class BindAddressParam {

    /**
     * 收件人
     */
    @Schema(description = "收件人")
    private String consignee;

    /**
     * 手机号
     */
    @Schema(description = "手机号")
    private String phone;

    /**
     * 省
     */
    @Schema(description = "省")
    private String province;

    /**
     * 市
     */
    @Schema(description = "市")
    private String city;

    /**
     * 区
     */
    @Schema(description = "区")
    private String district;

    /**
     * 详细地址
     */
    @Schema(description = "详细地址")
    private String detailAddress;

    /**
     * 是否默认
     */
    @Schema(description = "是否默认")
    private Boolean isDefault;


    /**
     * 校验
     */
    public void validate() {
        if (StringUtils.isEmpty(this.province)) {
            throw new BusinessException("省不能为空");
        }
        if (StringUtils.isEmpty(this.city)) {
            throw new BusinessException("市不能为空");
        }
        if (StringUtils.isEmpty(this.district)) {
            throw new BusinessException("区不能为空");
        }
        if (StringUtils.isEmpty(this.detailAddress)) {
            throw new BusinessException("详细地址不能为空");
        }
        if (StringUtils.isEmpty(this.consignee)) {
            throw new BusinessException("收件人不能为空");
        }
        if (StringUtils.isEmpty(this.phone)) {
            throw new BusinessException("联系人手机号不能为空");
        }
    }


    /**
     * 转换为po
     * @return
     */
    public UserAddress toPo() {
        UserAddress userAddress = new UserAddress();
        userAddress.setProvince(this.province);
        userAddress.setCity(this.city);
        userAddress.setDistrict(this.district);
        userAddress.setDetailAddress(this.detailAddress);
        userAddress.setConsignee(this.consignee);
        userAddress.setPhone(this.phone);
        userAddress.setIsDefault(BooleanUtils.isTrue(this.isDefault) ? ONE : ZERO);
        userAddress.setStatus(UserAddressStautsEnum.ENABLE.getCode());
        return userAddress;
    }
}
